package com.example.friendfindertwo.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;

import com.example.friendfindertwo.LoginActivity;
import com.example.friendfindertwo.R;
import com.example.friendfindertwo.adapter.FriendSearchListAdapter;

public class SearchFriendFragment extends ListFragment{

	
	public static final String ARG_PAGE = "ARG_PAGE";
	private int mPage;
	//private ArrayAdapter<String> adapter2 = null;
	private FriendSearchListAdapter adapter2;
	String[] friends = null;

	
	//private ListView listView;
	
	public static SearchFriendFragment create(int page){
		Bundle args = new Bundle();
		args.putInt(ARG_PAGE, page);
		SearchFriendFragment fragment = new SearchFriendFragment();
		fragment.setArguments(args);
		return fragment;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		mPage = getArguments().getInt(ARG_PAGE);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		View view = inflater.inflate(R.layout.find_friends_page, container, false);
		
		new UserDownloader().execute();

//		listView = (ListView) view.findViewById(android.R.id.list);
//		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//			public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
//                         long id) {
//				registerForContextMenu(listView);
//				Log.d("friends","on click list view add friend");
//				}
//		});

		
//		SearchView searchView = (SearchView) view.findViewById(R.id.search_friend);
//		
//		SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
//			@Override
//			public boolean onQueryTextChange(String newText) {
//				// Do something
//				if(newText != null){
////					listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
////						public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
////								long id) {
////							registerForContextMenu(listView);
////						}
////					});	
////					//adapter2.getFilter().filter(newText);
////					listView.setAdapter(adapter2);
//				}
//
//				return true;
//			}
//
//			@Override
//			public boolean onQueryTextSubmit(String query) {
//				// Do something
//				if(query != null){
//					//adapter2.getFilter().filter(query);
//					//listView.setAdapter(adapter2);
//				}
//				return true;		    
//			}
//		};
//		
//		searchView.setOnQueryTextListener(queryTextListener);
//		
//		
		return view;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		 AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) menuInfo;
	       
	      // We know that each row in the adapter is a Map
//	      String users =  (String) adapter2.getItem(aInfo.position);
	      String users = friends[aInfo.position];
	      menu.setHeaderTitle(users);
	      menu.add(1, 1, 1, "Add as a friend");
	}
	@Override
	public boolean onContextItemSelected(MenuItem item) {
	    int itemId = item.getItemId();
	    // Implements our logic
	    if (getUserVisibleHint()) {
	        // context menu logic
	        
	    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	    Log.d("friends","user to add : "+friends[info.position]);
	    LoginActivity.dataBaseObject.sendRequest((String) friends[info.position]);
	    return true;
	    }
	    return false;
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		registerForContextMenu(getListView());
	}
	
	private class UserDownloader extends AsyncTask<Void, String[], String[]>{
		String[] friends;
		ProgressDialog myPd_ring;
		
		@Override
        protected void onPreExecute(){
			super.onPreExecute();
            myPd_ring  = new ProgressDialog (getActivity());
            myPd_ring.setMessage("loading users");
            myPd_ring.show();
        }

		@Override
		protected String[] doInBackground(Void... params) {
			friends = new String[LoginActivity.dataBaseObject.getUser("").length];
	    	friends = LoginActivity.dataBaseObject.getUser("");
	    	SearchFriendFragment.this.friends = friends;
			return friends;
		}

		@Override
		protected void onPostExecute(String[] result) {
			super.onPostExecute(result);
			myPd_ring.dismiss();
			if(getActivity() != null){
				//adapter2 = new ArrayAdapter<String>(getActivity(), R.layout.find_friends_single, result);
				adapter2 = new FriendSearchListAdapter(getActivity(), result);
				getListView().setAdapter(adapter2);
			}
		}
	}


}
